home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / glibc108.gz / glibc108 / glibc-1.08.1 / manual / examples / sigh1.c < prev    next >
C/C++ Source or Header  |  1994-02-16  |  671b  |  37 lines

  1. #include <signal.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. /* This flag controls termination of the main loop. */
  6. volatile sig_atomic_t keep_going = 1;
  7.  
  8. /* The signal handler just clears the flag and re-enables itself. */
  9. void 
  10. catch_alarm (int sig)
  11. {
  12.   keep_going = 0;
  13.   signal (sig, catch_alarm);
  14. }
  15.  
  16. void 
  17. do_stuff (void)
  18. {
  19.   puts ("Doing stuff while waiting for alarm....");
  20. }
  21.  
  22. int
  23. main (void)
  24. {
  25.   /* Establish a handler for SIGALRM signals. */
  26.   signal (SIGALRM, catch_alarm);
  27.  
  28.   /* Set an alarm to go off in a little while. */
  29.   alarm (2);
  30.  
  31.   /* Check the flag once in a while to see when to quit. */
  32.   while (keep_going)
  33.     do_stuff ();
  34.  
  35.   return EXIT_SUCCESS;
  36. }
  37.